package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import client.global.commlib.dialog.ValidatePwdDialog
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.ShopingBean
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.api.bean.UserAddress
import com.kiln.juxingqs.databinding.ActivityConfirmorderBinding
import com.kiln.juxingqs.databinding.LayoutConfirmenterorderitemBinding
import com.kiln.juxingqs.ui.vm.ConfirmOrderModel
import java.math.BigDecimal

class ConfirmOrderActivity :
    BaseMiddleActivity<ConfirmOrderModel, ActivityConfirmorderBinding>() {

    var tradeNo: String = "" //订单编号

    override fun getLayout(): Int {
        return R.layout.activity_confirmorder
    }

    override fun initView() {
        binding?.apply {
            event = this@ConfirmOrderActivity
            vm = viewModel
            emptyLayout.setOnClickListener {
                viewModel.userInfo()
            }
        }
    }

    override fun observerUI() {
        viewModel.userRet.observe(this, androidx.lifecycle.Observer {

            it?.let {
                if (it.isSuccess) {

                    binding.emptyLayout.visibility = View.GONE
                    binding.user = it.data


                    it.data?.address_list?.let {
                        if (it.size > 0)
                            binding.addr = it[0]
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                viewModel.userRet.value = null

            }
        })
        viewModel.commitRet.observe(this, androidx.lifecycle.Observer {

            it?.let {
                if (it.isSuccess) {
                    toast("成功")
//                    startActivity(Intent(this, WdthActivity::class.java))
                    finish()
                } else {
                    toast(it.message)
                }
            }
        })

//        viewModel.userInfo()
    }

    override fun initViewModel(): ConfirmOrderModel = createViewModel()

    override fun initData() {
        val list = intent.getParcelableExtra<ShopingBean>("bean")
        list?.let { data ->
            binding.goodsWrap.removeAllViews()

            var type = 0
            val bindingType =
                DataBindingUtil.inflate<LayoutConfirmenterorderitemBinding>(
                    LayoutInflater.from(this),
                    R.layout.layout_confirmenterorderitem,
                    binding.goodsWrap,
                    false
                ).apply {
                    this.bean = data
                    this.num = intent.getStringExtra("num")

                    tvPrice.text = "￥${data.price}"
                }
            binding.goodsWrap.addView(bindingType.root)
            updatePrice()
        }
    }


    fun updatePrice() {
        var number: BigDecimal = BigDecimal(0)
//        var lpq_price_new: BigDecimal = BigDecimal(0)
        var integral: BigDecimal = BigDecimal(0)
        var integral_f: BigDecimal = BigDecimal(0)
        val items = intent.getParcelableExtra<ShopingBean>("bean")?.let {
            it?.let { b ->
                number =
                    number.add(BigDecimal(intent.getStringExtra("num")).multiply(BigDecimal(b.price)))
            }
        }
        binding.coupon?.let {
            number = number.subtract(BigDecimal(it.minus_price))
        }

        binding.price = "${number.toDouble()}"
        binding.integral = "${integral.toDouble()}"

        if (number.toDouble() > 0) {
            binding.tvPrice.text = "￥${number.toDouble()}"
            binding.tvPrice.visibility = View.VISIBLE
            binding.hjprice.text = "￥${number.toDouble()}"
            binding.price = "${number.toDouble()}"
            binding.rlGoodAccount.visibility = View.VISIBLE

        }


    }

    fun ok() {
//        if (binding.addr == null) {
//            toast("请选择地址")
//            return
//        }

        ValidatePwdDialog(this@ConfirmOrderActivity).apply {
            title = "需要验证支付密码"
            ok = {
                val list = intent.getParcelableExtra<ShopingBean>("bean")

                viewModel.commitOrderByPayPwdValid(
                    it,
                    intent.getStringExtra("num")!!,
                    list as ShopingBean,
                )

            }

        }.show()

    }

    fun selectAddr() {
        startActivityForResult(Intent(this, AddrMangerActivity::class.java).apply {
            putExtra("select", true)
        }, 0x11)
    }

    fun selectCoupon() {
        startActivityForResult(Intent(this, MeCouponActivity::class.java).apply {
            putExtra("select", true)
            putExtra("price", binding.price!!.toDouble())
        }, 0x12)
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            0x11 -> {
                if (resultCode == RESULT_OK) {
                    data?.let {
                        val addr = it.getSerializableExtra("address") as UserAddress.Address?
                        addr?.let {
                            binding.addr = it
                        }
                    }
                }
            }
        }
    }


}